/**
 * Created by jocky on 26/08/2017.
 */
var accountObj = {
    /**
     * 获取账户信息
     * @type int 1账户信息，2推广信息，3实名信息，4直属层级链信息，5成本信息
     */
    getAccountInfo: function (type) {
        var str = "" +
            "<div class='text-center'>" +
            "<img style='margin: 10px;width: 40px;height: 40px;' src='/images/loading.gif'/>" +
            "</div>";
        $("#account_info_" + type).html(str);

        $.get('/account/info', {type: type}, function (html) {
            $("#account_info_" + type).html(html);
        })
    },

    /**
     * 修改本账户信息
     */
    setForm: function () {
        $.get('/account/set-form', function (html) {
            $('#modal_pop_title').html('<i>账户设置</i>');
            $('#modal_pop_body').html(html);
            $(document).off('click', '#modal_pop_btn');
            $(document).on('click', '#modal_pop_btn', function () {
                accountObj.set();
            });

            $('#modal_pop').modal('show');

        })
    },

    /**
     * 提交修改
     */
    set: function () {
        var params = {};

        $.each($("#account_set_form").find("input"), function (i, v) {
            var name = $(v).attr('name');
            if (name) {
                params[name] = $(v).val();
            }
        });
        swal({
            title: '确定修改吗?',
            type: 'warning',
            showCancelButton: true,
            confirmButtonColor: '#3085d6',
            cancelButtonColor: '#d33',
            confirmButtonText: '确定',
            cancelButtonText: '取消'
        }).then(function () {
            $.post('/account/set', params, function (json) {
                var obj = {
                    title: '操作通知',
                    text: json.msg,
                    styling: 'bootstrap3',
                    delay: 5000,
                    type: ''
                };
                if (json.code == 1) {
                    $('#modal_pop').modal('hide');
                    accountObj.getAccountInfo(1);

                    obj.type = 'info';
                    obj.addclass = 'dark';
                } else {
                    obj.type = 'error';
                }
                new PNotify(obj);
            }, 'json')
        }, function (dismiss) {

        });
    },

    /**
     * 创建直属层级链
     * @returns {boolean}
     */
    addDirectLinks: function () {
        var params = {};
        var valid = true;
        $.each($("#activation_add_direct_links_form").find("input"), function (i, v) {
            var name = $(v).attr('name');
            if (name) {
                var val = $(v).val();
                if (name != 'alias' && !val) {
                    new PNotify({
                        title: '错误提醒',
                        text: '必填项，不能为空',
                        delay: 5000,
                        styling: 'bootstrap3'
                    });

                    $(v).focus();
                    valid = false;
                    return false;
                }
                params[name] = val;
            }
        });

        if (!valid) return false;

        $.post('/account/add-direct-links', params, function (json) {
            var obj = {
                title: '操作通知',
                text: json.msg,
                styling: 'bootstrap3',
                delay: 5000,
                type: ''
            };
            if (json.code == 1) {
                menu_detail['menu_15']['loadContent'] = true;
                get_content('menu_15');

                obj.type = 'info';
                obj.addclass = 'dark';
            } else {
                obj.type = 'error';
            }
            new PNotify(obj);
        }, 'json');
    },
};